/*******************************************************************************
  * @file                   USART.c
  * @Author:                MQjehovah                 mail:MQjehovah@hotmail.com
  * @version                1.0.0
  * @date                   2017.8.24
  * @brief                  
  ******************************************************************************
  * @attention
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "USART.h"
/* Definition ----------------------------------------------------------------*/

/* Functions -----------------------------------------------------------------*/
/*******************************************************************************
  * @brief  串口引脚配置            
  * @param  None              
  * @retval None              
  * @Note   None              
******************************************************************************/
void USART_GPIO_Config(USART_TypeDef *USARTx)
{
	if (USARTx == USART1)
	{
		GPIO_InitTypeDef GPIO_InitStructure;

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	}
	if (USARTx == USART2)
	{
		GPIO_InitTypeDef GPIO_InitStructure;

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	}
	if (USARTx == USART3)
	{
		GPIO_InitTypeDef GPIO_InitStructure;

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB, &GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOB, &GPIO_InitStructure);
	}
	//	if(USARTx == USART4)
	//	{
	//		GPIO_Config(&PA9,GPIO_Mode_AF_PP);
	//		GPIO_Config(&PA10,GPIO_Mode_IN_FLOATING);
	//	}
	//	if(USARTx == USART5)
	//	{
	//		GPIO_Config(&PA9,GPIO_Mode_AF_PP);
	//		GPIO_Config(&PA10,GPIO_Mode_IN_FLOATING);
	//	}
}

/*******************************************************************************
  * @brief  串口初始化            
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void USART_Config(USART_TypeDef *USARTx, u32 Buand)
{
	USART_InitTypeDef USART_InitStructure;

	USART_GPIO_Config(USARTx);
	USART_InitStructure.USART_BaudRate = Buand;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USARTx, &USART_InitStructure);
	USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
	USART_Cmd(USARTx, ENABLE);
}

/*******************************************************************************
  * @brief  串口发送一个字节              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void USART_SendChar(USART_TypeDef *USARTx, char c)
{
	USART_SendData(USARTx, c);
	while (RESET == USART_GetFlagStatus(USARTx, USART_FLAG_TXE))
		;
}
/*******************************************************************************
  * @brief  USART发送字符串
  * @param  None
  * @retval None
  * @Note   None
*******************************************************************************/
void USART_SendStr(USART_TypeDef *USARTx, char *senddata)
{
	while ('\0' != *senddata) // \0 表示字符串结束标志，通过检测是否字符串末尾
	{
		USART_SendData(USARTx, *senddata++);
		while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
			;
	}
}

/*******************************************************************************
  * @brief  打印数字，带进制转换
  * @param  None
  * @retval None
  * @Note   None
*******************************************************************************/
void printNumber(USART_TypeDef *USARTx, unsigned long n, u8 base)
{
	unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
	unsigned long i = 0;

	if (n == 0)
	{
		USART_SendChar(USARTx, '0');
		return;
	}

	while (n > 0)
	{
		buf[i++] = n % base;
		n /= base;
	}

	for (; i > 0; i--)
		USART_SendChar(USARTx, (char)(buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
}

/*******************************************************************************
  * @brief  串口3中断接收              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void USART1_IRQHandler(void) //串口1中断服务程序
{
	if ((USART_GetITStatus(USART1, USART_IT_RXNE) != RESET))
	{
		u8 data = USART_ReceiveData(USART1);
		if (RingBuf_write(pSHELL_RX_BUF, data))
			;
	}
	//  USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}

//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
	int handle;
};

FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
	x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
	USART_SendChar(USART_DEBUG, ch);
	return ch;
}
#endif

/*********************************END OF FILE**********************************/
